﻿using System.Windows;
using System.Windows.Input;
using Red.Controls;
using Red.Core.GoogleAnalytics;

namespace Red.Launcher
{
    public partial class LauncherWindow
    {
        #region Private Data

        private const string ANALYTICS_CATEGORY = "Main Window";
        private const string BASE_SCREEN_ID = "MainWindow";

        private string _currentScreen = BASE_SCREEN_ID;

        #endregion Private Data

        #region C-Tor

        partial void InitializeAnalytics()
        {
            _tiles.RegisterClickCallback( "main", AnalyticsTileMain_Click );
            _tiles.RegisterClickCallback( "application", AnalyticsTileApplication_Click );
            _settingsFlyout.ClosingFinished += _settingsFlyout_ClosingFinished;

            CommandBindings[ 0 ].Executed += AnalyticsDocumentation_Executed;

            Activated += LauncherWindow_Activated;

            _packagesFlyout.IsOpenChanged += _flyout_IsOpenChanged;
            _workspacesFlyout.IsOpenChanged += _flyout_IsOpenChanged;
            _targetsFlyout.IsOpenChanged += _flyout_IsOpenChanged;
            _settingsFlyout.IsOpenChanged += _flyout_IsOpenChanged;
            _externalAppsFlyout.IsOpenChanged += _flyout_IsOpenChanged;
            _settingsFlyout.IsOpenChanged += _flyout_IsOpenChanged;
        }

        #endregion C-Tor

        #region Event Handlers

        private void AnalyticsTileMain_Click( string function )
        {
            switch( function )
            {
                case "toolkit":
                    GoogleAnalyticsService.Instance.SendEvent( "Toolkit Tile", ANALYTICS_CATEGORY, _packagesView.ActiveToolkitOrEditorManager.ActivePackageName );
                    break;

                case "game":
                    GoogleAnalyticsService.Instance.SendEvent( "Run Game Tile", ANALYTICS_CATEGORY, _packagesView.EnginePackagesControl.Manager.ActivePackageName );
                    break;

                case "packages":
                    GoogleAnalyticsService.Instance.SendEvent( "Package Manager Tile", ANALYTICS_CATEGORY );
                    break;

                case "workspaces":
                    GoogleAnalyticsService.Instance.SendEvent( "Workspaces Tile", ANALYTICS_CATEGORY );
                    break;

                case "targets":
                    GoogleAnalyticsService.Instance.SendEvent( "Targets Tile", ANALYTICS_CATEGORY );
                    break;

                case "apps":
                    GoogleAnalyticsService.Instance.SendEvent( "External Applications Tile", ANALYTICS_CATEGORY );
                    break;

                case "recent":
                    GoogleAnalyticsService.Instance.SendEvent( "Recent Files Tile", ANALYTICS_CATEGORY );
                    break;

                case "settings":
                    GoogleAnalyticsService.Instance.SendEvent( "Settings Tile", ANALYTICS_CATEGORY );
                    break;

                case "get_latest_install":
                    GoogleAnalyticsService.Instance.SendEvent( "Get Latest Tile", ANALYTICS_CATEGORY );
                    break;

                case "scripts":
                    GoogleAnalyticsService.Instance.SendEvent( "Run Scripts Editor Tile", ANALYTICS_CATEGORY );
                    break;
            }
        }

        private void AnalyticsTileApplication_Click( string function )
        {
            GoogleAnalyticsService.Instance.SendEvent( "Custom Application Tile", ANALYTICS_CATEGORY, function );
        }

        void _settingsFlyout_ClosingFinished( object sender, RoutedEventArgs e )
        {
            _settingsView.SendAnalytics();
        }

        private void AnalyticsDocumentation_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            GoogleAnalyticsService.Instance.SendEvent( "Open Confluence Documentation", ANALYTICS_CATEGORY );
        }

        private void _flyout_IsOpenChanged( object sender, RoutedEventArgs e )
        {
            Flyout flyout = sender as Flyout;

            string newScreen = ( flyout.IsOpen ) ? flyout.Header : BASE_SCREEN_ID;

            if ( _currentScreen != newScreen )
            {
                _currentScreen = newScreen;
                GoogleAnalyticsService.Instance.SetScreen( _currentScreen );
            }
        }

        private void LauncherWindow_Activated( object sender, System.EventArgs e )
        {
            GoogleAnalyticsService.Instance.SetScreen( _currentScreen );
        }

        #endregion Event Handlers

        #region Protected Methods

        protected override GoogleAnalyticsContext GetGoogleAnalyticsContext()
        {
            return new GoogleAnalyticsContext( "Launcher", "UA-42984990-8" );
        }

        #endregion Protected Methods
    }
}
